package xyz.zcq.zcqrpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * 模拟调用服务代理（JDK动态代理）
 */
@Slf4j
public class MockServiceProxy implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Class<?> returnType = method.getReturnType();
        log.info("mock invoke {}", method.getName());
        return getDefaultObject(returnType);
    }

    private static Object getDefaultObject(Class<?> returnType) {
        if (returnType.isPrimitive()) {
            if (Objects.equals(returnType, boolean.class)) {
                return false;
            } else if (Objects.equals(returnType, short.class)) {
                return (short) 0;
            } else if (Objects.equals(returnType, int.class)) {
                return 0;
            } else if (Objects.equals(returnType, long.class)) {
                return 0L;
            }
        }
        return null;
    }

}
